using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SpuspecificationvalueController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Spuspecificationvalue> _SpuspecificationvalueRepository;
        private TokenParameter _tokenParameter;

        public SpuspecificationvalueController(IConfiguration configuration, IRepository<Spuspecificationvalue> SpuspecificationvalueRepository)
        {
            _configuration = configuration;
            _SpuspecificationvalueRepository = SpuspecificationvalueRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出商品列表
        [HttpGet]
        public dynamic Get()
        {
            var Spuspecificationvalue = _SpuspecificationvalueRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = Spuspecificationvalue },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var spuspecificationvalue = _SpuspecificationvalueRepository.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spuspecificationvalue,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateSpuspecificationvalue newSpuspecificationvalue)
        {
            var Spuspecificationvaluename = newSpuspecificationvalue.Spuspecificationvaluename.Trim();
            var Spuspecificationvalue = new Spuspecificationvalue
            {
                Spuspecificationvaluename = newSpuspecificationvalue.Spuspecificationvaluename,
                SpuspecificationId = newSpuspecificationvalue.SpuspecificationId,
                SkuId = newSpuspecificationvalue.SkuId
            };
            _SpuspecificationvalueRepository.Insert(Spuspecificationvalue);
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Spuspecificationvalue,
                Msg = "添加商品规格值成功"
            });
        }

        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSpuspecificationvalue updateSpuspecificationvalue)
        {
            var Spuspecificationvaluename = updateSpuspecificationvalue.Spuspecificationvaluename.Trim();
            var spuspecificationvalue = _SpuspecificationvalueRepository.GetById(id);
            if (spuspecificationvalue == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            spuspecificationvalue.Spuspecificationvaluename = updateSpuspecificationvalue.Spuspecificationvaluename;
            spuspecificationvalue.SpuspecificationId = updateSpuspecificationvalue.SpuspecificationId;
            // user.IsActived= updateUser.IsActived;

            _SpuspecificationvalueRepository.Update(spuspecificationvalue);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spuspecificationvalue,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _SpuspecificationvalueRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
    }

}
